

var products = {};
var type="product";
var not_check_label = 0;
//直接<{$lang.print}> 
function printPage(){
 document.getElementById("WebBrowser").ExecWB(6,1);
 }

// <{$lang.print}>设置
 function printSetup(){
  document.getElementById("WebBrowser").ExecWB(8,1);
 }

// <{$lang.print}><{$lang.view}>
 function printPreview(){
  document.getElementById("WebBrowser").ExecWB(7,1);
 }
 
 var isIE = 0;
 if (+[1,]){//非IE
 	isIE = 0;	
 }else {//IE
 	isIE = 1;
 	
 }

var timeout = 0;

var ship_method = "";
$(function(){
//loading();
});
function printFp()
{		
//	if(isIE&&ship_method!="EPACKET"){
//		alertTip("请使用firefox，该<{$lang.function}>在ie下 仅支持EPACKET");
//		return false;
//	}
	loading();
	var tn=$("#tracking_number").val();
	if(ship_method=="SELFPICKUP"){
		tn = ship_method;
	}
	if(tn==''){return;}
	$("#pdfIframeWrap").hide();
	$.ajax({
        type: "POST",
        async: false,
        dataType: "html",
        url: "/order/order/print-pop/code/"+tn+"/ordersCode/"+$("#orders_code").val(),
        success: function(json) {
        	
        	var ask = json.charAt(0);

    		var reg=new RegExp("^[0-1]","g"); //<{$lang.create}>正则RegExp对象 
    		json=json.replace(reg,""); 
        	var html = "";
        	if(ask==1){
        		html = json;
        		var link = "";

            		var  pwin=window.open("a.html","print",'height=200, width=200, top=-200, left=-200, toolbar=no, menubar=no, scrollbars=yes,resizable=yes');   
                 	pwin.document.write(html);   
                  	pwin.document.close();             //这句很重要哦  
                 	
                 	if(isIE){
        	         	pwin.document.all.WebBrowser.ExecWB(6,2);
                 	}else{
                 		pwin.print();	         		
                 	}	
                 	
         			closeLoading();
                 	link = "/order/order/print-pop/code/"+tn+"/ordersCode/"+$("#orders_code").val()+"/js/no/reprint/1";

             	$("#print_result").append("<div><a href='"+link+"' target='_blank'><{$lang.orders_code}>："+$("#orders_code").val()+"<{$lang.shipping_code}>："+tn+"<{$lang.print}></a></div>");
    			
    			
    		}else{
    			$('<div title="<{$lang.tip}>"><p align="left">'+json+'</p></div>').dialog({
    				modal : true,
    				width : 200,
    				buttons : {
    					'<{$lang.close}>' : function(){
    						$(this).dialog("close");
    					}
    				}
    			});
         		closeLoading();
    		}
			
        }
	}); 
}	
function clear(){

	var allok = true;
	$("#order_products tr").each(function(){
		var quantity = $.trim($(this).children(".quantity").text());
		var quantity1 = $.trim($(this).children("td:last").text());
		if(quantity!=quantity1){
			allok = false;
		}
	});
	if(allok||not_check_label==1){
			//$("#orders_code_ch").val("");
	 	$("#product_sku").val("");
	 	//$("#order_products").html("");
	 	$("#pp_name").html("");
	 	setTimeout(function(){$("#orders_code_ch").focus().select();},50);
	 	
	}        
}

function myfocus(){
	$("#orders_code_ch").focus();	
}
window.onload = myfocus;
$(function () {
	$("#printDialog").dialog({
        autoOpen:false,
        modal:true,
        width:400,
        title:'PDF<{$lang.print}>……',
        show:"slide",
        buttons:{
            'Close':function () {
                $(this).dialog('close');
            }
        }
    });
    $("#orders_code_ch_btn").click(function () {    	
    	
    	var orders_code_ch = $.trim($("#orders_code_ch").val());    	
        if(orders_code_ch==""){
        	type = "order";
        	alertTip("<{$lang.pls_input}><{$lang.orders_code}>");
        	return false;
        }
        $("#order_products").html("");
        loading();
        $.ajax({
            type: "post",
            async: false,
            dataType: "json",
            url: "/warehouse/print-logistics/order-product/orders_code/"+orders_code_ch,
           
            success: function(json) {
            	closeLoading();
            	products = {};
            	$("#orders_code_ch").select();
            	if(json.ask==1){       		
            		var html = "";
            		ship_method = json.order.shipping_method;

            		$.each(json.products,function(k,v){
            			html+="<tr id='"+v.customer_id+v.product_sku+"'>";
            			products[v.customer_id+v.product_sku] = v.customer_id+v.product_sku;
            			html+="<td ondblclick='quantity_add(this,"+v.op_id+");'>"+v.customer_id+"-"+v.product_sku+"</td>";
            			html+="<td title="+v.product_id+">"+v.title_cn+"</td>";
            			html+="<td>"+v.title+"</td>";
						html+="<td>";

            			html+="<img src='/product/product/view-picture/id/"+v.product_id+"' height='75'>";
//						$.each(v.pic,function(pk,pv){
//							html+='<div class="product-img-view" '+(pk!=0 ? 'style="display:none;"' : '')+'><a class="fancybox-thumbs" data-fancybox-group="thumb" href="/product/product/view-qc-picture/paid/'+pv+'"><img style="width:75px;height:75px;" src="/product/product/view-qc-picture/paid/'+pv+'"></a></div>';
//						});
						html+="</td>";
            			html+="<td class='quantity'>"+v.op_quantity+"</td>";
            			html+="<td  ondblclick='quantity_del(this);'>0"+"</td>";            			
            			
            			html+="</tr>";
            			
            		})
            		$("#order_products").html(html);
            		$("#tracking_number").val(json.tracking_number);
            		$("#orders_code").val(orders_code_ch);
            		$("#pp_name").text(json.pp_name);
            		not_check_label = json.not_check_label;
                    printFp();
            		if(not_check_label==1){//直接<{$lang.print}>
            			clear();
            			return;
            		}

                	$("#product_sku").focus();

            	}else{
                	$("#orders_code_ch").focus();
                	type = "order";
            		alertTip(json.msg);
            	}
            	
            	
                	
            	
            }
        });
    }).focus(function(){
    	$("#orders_code_ch").select();
     });

    $("#product_sku_btn").click(function(){
	    	
    		type="product";
			var sku = $.trim($("#product_sku").val());
			sku = sku.replace("-","");
			$("#product_sku").select();
			if(sku==""||!products[sku]){
				alertTip("<{$lang.orders_code}>SKU<{$lang.exception}>");
				return false;
			}
			var td = $("#"+sku).children("td:last");
			var quantity = parseInt(td.text());
			quantity++;

			var td1 = $("#"+sku).children(".quantity");
			var quantity1 = parseInt(td1.text());
			if(quantity>quantity1){
				alertTip("<{$lang.quantity}><{$lang.is_max}>");
				return false;
			}
			td.text(quantity);
			clear();
        });
  //回车处理
	$("#orders_code_ch").keyup(function(e){
		var key = e.which;
		if(key==13){
			$("#orders_code_ch_btn").click();
			$("#orders_code_ch").blur();
		}
	});
	 //回车处理
	$("#product_sku").keyup(function(e){
		var key = e.which;
		if(key==13){
			$("#product_sku_btn").click();
		}
	});
	
});

//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>(<{$lang.close}><{$lang.esc}>)"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,        
        close:function(){
        	if(type=="order"){
            	$("#orders_code_ch").focus();        		
        	}else{
            	$("#product_sku").focus();
        		
        	}
        }
    });
}

//闪烁
function bling(obj){
	$(obj).parent().css("color","red");
	setTimeout(function(){
		$(obj).parent().css("color","#333");
		},500);
}
function quantity_add(obj,op_id){
	$("#product_sku").focus();      
	var quantity = $(obj).parent().children("td:last").text();
	quantity = parseInt(quantity);
	quantity++;
	if(quantity>parseInt($(obj).siblings(".quantity").text())){
		alertTip("<{$lang.quantity}><{$lang.is_max}>");
		return false;
	}
	$(obj).parent().children("td:last").text(quantity);
	bling(obj);
	clear();
}

function quantity_del(obj){
	$("#product_sku").focus();      
	var quantity = $(obj).text();
	quantity = parseInt(quantity);
	quantity--;
	if(quantity<0){
		alertTip("<{$lang.quantity}><{$lang.is_min}>");
		return false;
	}
	$(obj).text(quantity);
	bling(obj);
}